define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiluhouse/user/lessor/index',
                    detail_url: 'xiluhouse/user/lessor/detail',
                    edit_url: 'xiluhouse/user/lessor/edit',
                    del_url: 'xiluhouse/user/lessor/del',
                    multi_url: 'xiluhouse/user/lessor/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'gender', title: __('Gender'), visible: false, searchList: { 1: __('Male'), 2: __('Female'), 0: __('未知') }, formatter: Table.api.formatter.normal },
                        {field: 'money', title: __('Money'), operate: 'BETWEEN', sortable: true},
                        {field: 'userprofile.deal_num', title: __('deal_num'), operate: 'BETWEEN', sortable: true},
                        { field: 'userprofile.is_broker', title: __('Is_broker'), searchList: { '0': __('Is_broker 0'), '1': __('Is_broker 1'), '2': __('Is_broker 2') }, formatter: Table.api.formatter.normal },
                        {field: 'userprofile.pick_num', title: __('pick_num'), operate: 'BETWEEN', sortable: true},
                        {field: 'userprofile.abandon_num', title: __('abandon_num'), operate: 'BETWEEN', sortable: true},
                        {field: 'userprofile.deal_percent', title: __('deal_percent'), operate: 'BETWEEN', sortable: true},
                        {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        { field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search },
                        {field: 'userprofile.weigh', title: __('Weigh'),sortable:true},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                        buttons: [], formatter: Table.api.formatter.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var buttons = that.buttons;
                                buttons[0]={
                                    name: 'detail', 
                                    text: '', 
                                    title: '查看详情',
                                    icon: 'fa fa-eye',
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    url: 'xiluhouse/user/user/detail?ids='+row.id
                                };
                                that.buttons = buttons;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});